/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "vectorBuffer.h"
#include <iostream>
#include "../tools/log.h"

using namespace std;


void VectorBuffer::clear()
{
  	_vector.clear();
}


Message* VectorBuffer::first()
{
	if ( isEmpty() ) return NULL;
	else
	{

		return _vector.front();
	}
}
Message* VectorBuffer::last()
{
	if ( isEmpty() ) return NULL;
	else return _vector.back();
}



Message* VectorBuffer::next ( Message* message )
{
	vector<Message*>::iterator it;
	for ( it=_vector.begin() ; it < _vector.end(); it++ )
		if ( **it==*message )
		{
			it++;
			return *it;
		}
	return NULL;
}

Message* VectorBuffer::prev ( Message* message )
{
	vector<Message*>::reverse_iterator rit;
	for ( rit=_vector.rbegin() ; rit < _vector.rend(); ++rit )
		if ( **rit==*message )
		{
			rit++;
			return *rit;
		}
	return NULL;
}

bool VectorBuffer::isEmpty()
{
	return _vector.empty();
}

unsigned int VectorBuffer::size()
{
	return _vector.size();
}

bool VectorBuffer::contains ( Message* message )
{
	vector<Message*>::iterator it;
	for ( it=_vector.begin() ; it < _vector.end(); it++ )
	{
		if ( **it==*message )
			return true;
	}
	return false;
}

void VectorBuffer::insert ( Message* message )
{
	FILE_LOG(logDEBUG)<<"VectorBuffer::insert"<<endl;
	_vector.push_back ( message );
}

void VectorBuffer::remove ( Message* message )
{
	vector<Message*>::iterator it;
	for ( it=_vector.begin();it<_vector.end();it++ )
	{
		if ( **it==*message )
		{
			_vector.erase ( it );
			break;
		}
	}

}
ostream& operator<< ( ostream& out,VectorBuffer vb )
{

	vector<Message*>::iterator it;
	for ( it=vb._vector.begin();it<vb._vector.end();it++ )
	{
		out<<*it;
	}
	return out;
}

